﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Leo.Entities;


namespace Leo.Repository
{
    public class ProveedorRepository :MasterRepository, IProveedorRepository
    {
        public List<Proveedor> GetAllFromProveedores()
        {
            var query = from p in context.Proveedors
                        select p;
            return query.ToList();
                        
        }

        public List<Proveedor> GEtFromProveedorByCriterio(string criterio)
        {
            var query = from p in context.Proveedors
                        select p;
            if (criterio!=null && criterio!=string.Empty)
            {
                query = from p in query
                        where p.RazonSocial.ToUpper().Contains(criterio.ToUpper()) ||
                        p.RUC.ToUpper().Contains(criterio.ToUpper()) ||
                        p.DescProductos.ToUpper().Contains(criterio.ToUpper())
                        select p;                                     
                       
            }
            return query.ToList();
        }

        public Proveedor GetFromProveedorById(int id)
        {
            return context.Proveedors.Find(id);
        }

        public bool ExisteDuplicidadProveedor(string ruc, string razonSocil, int id)
        {
            var query = from p in context.Proveedors
                        where (p.RUC.ToUpper().CompareTo(ruc.ToUpper()) == 0 &&
                        p.RazonSocial.ToUpper().CompareTo(razonSocil.ToUpper()) == 0) &&
                        p.Id != id
                        select p;
            return query.Any();
        }


        public void AddProveedor(Proveedor proveedor)
        {
            context.Proveedors.Add(proveedor);
            context.SaveChanges();
        }

        public void UpdateProveedor(Proveedor proveedor)
        {
            context.Entry(proveedor).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void RemoveProveedor(int id)
        {
            var existe = context.Proveedors.Find(id);
            if (existe!=null)
            {
                context.Proveedors.Remove(existe);
                context.SaveChanges();
            }
        }
    }
}
